package com.adingxiong.cft.util;

import com.adingxiong.cft.constant.DateFormatEnum;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName Date
 * @Description 时间工具类
 * @Author xiongchao
 * @Date 2020/7/23 17:43
 **/
public class DateUtils {

    private static SimpleDateFormat sdf = new SimpleDateFormat(DateFormatEnum.getSDF("ymd"));

    private static SimpleDateFormat week = new SimpleDateFormat(DateFormatEnum.getSDF("weeek"));

    /**
     *   获取两个时间之内的所有日期
     * @param startDate
     * @param endDate
     * @return
     */
    private static List<String> getBetweenDays(String startDate,String endDate){
        Date min = null;
        Date max = null;
        try {
            min = sdf.parse(startDate);
            max = sdf.parse(endDate);

            List<String> days = new ArrayList<>();
            Calendar start = Calendar.getInstance();
            start.setTime(min);
            while (start.getTimeInMillis() <= max.getTime()){
                days.add(sdf.format(start.getTime()));
                start.add(Calendar.DAY_OF_MONTH,1);
            }
            return days;
        } catch (ParseException e) {
            e.printStackTrace();
            return  null;
        }
    }


    /**
     * 获取指定日期的工作日
     * @param month
     * @param workDays
     * @return
     */
    private static Map<String,Object> getOutHoliday(String month ,String workDays){

        Map<String,Object> res  = new HashMap<>();
        String arr [] = month.split("-");
        Integer y = Integer.parseInt(arr[0]);
        Integer m = Integer.parseInt(arr[1]);

        Calendar calendar = Calendar.getInstance();
        calendar.set(y,m-1,1);

        List<String> days = new ArrayList<>();
        List<String> weekAr = Arrays.asList(workDays.split(","));

        while (calendar.get(Calendar.MONTH) < m){
            Date date = calendar.getTime();
            String currSun = week.format(date);

            weekAr.forEach(e ->{
                if(e.equals(currSun)){
                    days.add(sdf.format(calendar.getTime().clone()));
                }
            });
            calendar.add(Calendar.DATE,1);
            Integer year = calendar.get(Calendar.YEAR);
            if(!year.equals(y)){
                break;
            }
        }
        res.put(month,days);
        return res;
    }


}
